package com.xu.ai.chatclient.controller;

import com.alibaba.cloud.ai.dashscope.image.DashScopeImageModel;
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xu.ai.chatclient.util.AiRestUtil;
import com.xu.ai.chatclient.util.ServletUtil;

/**
 * ImageModel Controller
 *
 * @author xuguan
 * @since 2025/10/15
 */
@RestController
@RequestMapping("/api/image")
public class ImageController {

	private static final Logger log = LoggerFactory.getLogger(ImageController.class);

	private final DashScopeImageModel dashScopeImageModel;

	public ImageController(DashScopeImageModel dashScopeImageModel) {
		this.dashScopeImageModel = dashScopeImageModel;
	}

	/**
	 * 通义千问-文生图
	 */
	@GetMapping(value = "/qwen")
	public void qwenImage() {
		var dashScopeImageOptions = DashScopeImageOptions.builder().withModel("qwen-image-plus").withN(1).build();
		var imagePrompt = new ImagePrompt("日本动画角色叮铛猫哆唻A梦, 使用macbook认真的编写代码", dashScopeImageOptions);

		final ImageResponse imageResponse = dashScopeImageModel.call(imagePrompt);
		AiRestUtil.checkResponseMetadata(imageResponse.getMetadata());
		final String url = imageResponse.getResult().getOutput().getUrl();
		log.info("url: {}", url);

		ServletUtil.showImageResponse(url);
	}

	/**
	 * 通义万相-文生图V1
	 */
	@GetMapping(value = "/wanx-v1")
	public void wanxV1() {
		var dashScopeImageOptions = DashScopeImageOptions.builder()
			.withModel("wanx-v1")
			.withN(1)
			.withWidth(1024)
			.withHeight(1024)
			.withStyle("<photography>")
			.build();
		var imagePrompt = new ImagePrompt("18岁的中国女孩，笑容中带有一丝腼腆, 举止端庄, 落落大方, 精致的五官, 魔鬼的身材, 漂亮至极, 商业摄影, 室外, 电影级光照, 锐利的边缘.",
				dashScopeImageOptions);

		final ImageResponse imageResponse = dashScopeImageModel.call(imagePrompt);
		AiRestUtil.checkResponseMetadata(imageResponse.getMetadata());
		final String url = imageResponse.getResult().getOutput().getUrl();
		log.info("url: {}", url);

		ServletUtil.showImageResponse(url);
	}

	/**
	 * 通义万相-文生图V2
	 */
	@GetMapping(value = "/wanx-v2")
	public void wanxV2() {
		var dashScopeImageOptions = DashScopeImageOptions.builder()
			.withModel("wan2.5-t2i-preview")
			.withN(1)
			.withWidth(1024)
			.withHeight(1024)
			.withStyle("现代化的")
			.build();
		var imagePrompt = new ImagePrompt("一幅现代版的清明上河图, 充满现代元素", dashScopeImageOptions);

		final ImageResponse imageResponse = dashScopeImageModel.call(imagePrompt);
		AiRestUtil.checkResponseMetadata(imageResponse.getMetadata());
		final String url = imageResponse.getResult().getOutput().getUrl();
		log.info("url: {}", url);

		ServletUtil.showImageResponse(url);
	}

	/**
	 * 通义万相-通用图像编辑2.1
	 */
	@GetMapping(value = "/image-edit-wanx-v2.1")
	public void imageEditWanx2_1() {
		var dashScopeImageOptions = DashScopeImageOptions.builder()
			.withModel("wanx2.1-imageedit")
			.withFunction("stylization_all") // 全局风格化
			.withBaseImageUrl("http://wanx.alicdn.com/material/20250318/stylization_all_1.jpeg")
			.withN(1)
			.build();
		var imagePrompt = new ImagePrompt("转换成法国绘本风格", dashScopeImageOptions);

		final ImageResponse imageResponse = dashScopeImageModel.call(imagePrompt);
		AiRestUtil.checkResponseMetadata(imageResponse.getMetadata());
		final String url = imageResponse.getResult().getOutput().getUrl();
		log.info("url: {}", url);

		ServletUtil.showImageResponse(url);
	}

	/**
	 * 通义万相-涂鸦作画
	 */
	@GetMapping(value = "/image-sketch-wanx-v2.1")
	public void imageSetchWanx2_1() {
		var dashScopeImageOptions = DashScopeImageOptions.builder()
			.withModel("wanx-sketch-to-image-lite")
			.withSketchImageUrl("https://help-static-aliyun-doc.aliyuncs.com/assets/img/zh-CN/6609471071/p743851.jpg")
			.withN(1)
			.withWidth(768)
			.withHeight(768)
			// 输入草图对输出图像的约束程度, 取值范围为0-10，取值间隔为1， 默认值为10。取值越大表示输出图像跟输入草图越相似。
			.withSketchWeight(3)
			.withStyle("<watercolor>")
			.build();
		var imagePrompt = new ImagePrompt("一棵参天大树", dashScopeImageOptions);

		final ImageResponse imageResponse = dashScopeImageModel.call(imagePrompt);
		AiRestUtil.checkResponseMetadata(imageResponse.getMetadata());
		final String url = imageResponse.getResult().getOutput().getUrl();
		log.info("url: {}", url);

		ServletUtil.showImageResponse(url);
	}

	/**
	 * 通义万相-图像局部重绘
	 */
	@GetMapping(value = "/image-mask-wanx-x-painting")
	public void imageMaskWanxXPainting() {
		var dashScopeImageOptions = DashScopeImageOptions.builder()
			.withModel("wanx-x-painting")
			.withBaseImageUrl(
					"http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/source3.jpg")
			// 用户标记涂抹区域的图像URL地址，需要和base_image_url图像分辨率保持一致。不支持填写图像Base64数据。
			.withMaskImageUrl(
					"http://synthesis-source.oss-accelerate.aliyuncs.com/lingji/validation/mask2img/demo/glasses.png")
			.withN(1)
			.withWidth(1024)
			.withHeight(1024)
			.withStyle("<chinese painting>")
			.withMaskColor(new Integer[][] { { 0, 0, 0 }, { 134, 134, 134 } })
			.build();
		var imagePrompt = new ImagePrompt("一只狗戴着红色眼镜", dashScopeImageOptions);

		final ImageResponse imageResponse = dashScopeImageModel.call(imagePrompt);
		AiRestUtil.checkResponseMetadata(imageResponse.getMetadata());
		final String url = imageResponse.getResult().getOutput().getUrl();
		log.info("url: {}", url);

		ServletUtil.showImageResponse(url);
	}

}
